<?php 
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Form/UserFormElements.php
 *
 * This is a helper class providing configured form elements
 * for user forms via static methods.
 *
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 * @package GEM_Form
 */
class GEM_Form_UserFormElements
{
	/**
	 * Returns a user id element
	 * 
	 * @param string $form
	 * @return Zend_Form_Element_Hidden
	 */
	public static function id($form)
	{
		$id = new Zend_Form_Element_Hidden("{$form}_id");
		$id->setDecorators(array('ViewHelper'))
			->addValidator('Int',false, array('messages' => array('notInt' => 'Id must be an integer.')));
		return $id;
	}
	/**
	 * Returns username element
	 * 
	 * @param string $form | The form name
	 * @param boolean $validateUnique = false | Ensure the usename is unique?
	 * @return Zend_Form_Element_Text
	 */
	public static function username($form,$validateUnique = false)
	{
		$username = new Zend_Form_Element_Text("{$form}_username");
		$username->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
				->setFilters(array('StringTrim'))
				->setLabel('Username:')
				->setRequired(true)
				->addValidator('Alnum',true,array(
					'allowWhiteSpace' => true, 
					'messages' => array(
						'notAlnum' => 'Username must be alpha-numeric.'
			  			//'stringEmpty' => 'Please enter a username.'
			  	)))
			  	->addValidator('StringLength',false,array(
			  		0,50,
			  		'messages' => array(
			  		'stringLengthTooLong'=>'Username too long. Please keep under %max% characters.'
			  	)));
		if($validateUnique === true) $username->addValidator('UniqueUsername');
		return $username;
	}
	
	/**
	 * Returns fullname element
	 * 
	 * @param string $form | The form name
	 * @return Zend_Form_Element_Text
	 */
	public static function fullname($form)
	{
		$fullname = new Zend_Form_Element_Text("{$form}_fullname");
		$fullname->setFilters(array('StringTrim'))
				->setLabel('Fullname:')
				->setRequired(true)
				->addValidator('Alnum',true,array(
					'allowWhiteSpace' => true, 
					'messages' => array(
						'notAlnum' => 'Name must be alpha-numeric.'
			  			//'stringEmpty' => 'Please enter fullname.',
			  	)))
			  	->addValidator('StringLength',false,array(
			  		0,100,
			  		'messages' => array(
			  		'stringLengthTooLong'=>'Name too long. Please keep under %max% characters.'
			  	)));
		return $fullname;
	}	  	
	
	/**
	 * Returns email element
	 * 
	 * @param string $form | The form name
	 * @param boolean $validateUnique = false | Ensure that the email is unique?
	 * @return Zend_Form_Element_Text
	 */
	public static function email($form, $validateUnique = false)
	{
		$email = new Zend_Form_Element_Text("{$form}_email");
		$email->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
			  ->setRequired(true)
			  ->setLabel('Email:')
			  ->setFilters(array('StringTrim', 'StringToLower'))
			  ->addValidator('EmailAddress',true,array(
			  		'messages' => array(
			  			'emailAddressInvalid' => 'Email invalid.',
			  			'emailAddressInvalidFormat' => 'Invalid email format.',
			  			'emailAddressInvalidHostname' => 'Invalid hostname.'
			  )))
			  ->addValidator('StringLength',false,array(0,100,
			  		'messages' => array('stringLengthTooLong'=>'Email too long (%max% characters).')
			  ));
		if($validateUnique)
			$email->addValidator('UniqueEmail');
		return $email;
	}
	
	/**
	 * Returns confirm email element
	 * 
	 * @param string $form | The form name
	 * @param boolean $required = true
	 * @return Zend_Form_Element_Text
	 */
	public static function confirmEmail($form,$required = true)
	{
		$email = new Zend_Form_Element_Text("{$form}_confirmEmail");
		$email->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
			  ->setRequired($required)
			  ->setLabel('Confirm Email:')
			  ->setFilters(array('StringTrim', 'StringToLower'))
			  ->addValidator('EmailAddress',true,array(
			  		'messages' => array(
			  			'emailAddressInvalid' => 'Email invalid.',
			  			'emailAddressInvalidFormat' => 'Invalid email format.',
			  			'emailAddressInvalidHostname' => 'Invalid hostname.'
			  )))
			  ->addValidator('StringLength',false,array(0,100,
			  		'messages' => array('stringLengthTooLong'=>'Email too long (%max% characters).')
			  ))
			  ->addValidator('Confirm',false,array("{$form}_email",'messages' => array(
					'notMatch' => 'Email values must match.'
				)));
		return $email;
	}
	
	/**
	 * Returns info element
	 * 
	 * @param string $form | The form name
	 * @return Zend_Form_Element_Textarea
	 */
	public static function info($form)
	{
		$info = new Zend_Form_Element_Textarea("{$form}_info");
		$info->setLabel('Some information about you:')
			->setFilters(array('StringTrim'))
			->addValidator('StringLength',false,array(0,5000, 
				'messages' => array('stringLengthTooLong'=> 'Please keep info under %max% characters. You currently have %value% characters.')
			));
		return $info;
	}
	
	/**
	 * Returns locale element
	 * 
	 * @param string $form | The form name
	 * @return Zend_Form_Element_Select
	 */
	public static function locale($form)
	{
		$locale = new Zend_Form_Element_Select("{$form}_locale");
		$locale->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
				->setLabel('Select Language')
				->addMultiOption('en_US', 'English, United States')
				->addMultiOption('en_GB', 'English, Great Britain')
				->addMultiOption('en_AU', 'English, Australia')
				->setRequired(true)
				->addValidator('Locale');
		return $locale;
	}
	
	/**
	 * Returns timezone element
	 * 
	 * @param string $form | The form name
	 * @return Zend_Form_Element_Select
	 */
	public static function timezone($form)
	{
		$timezone = new Zend_Form_Element_Select("{$form}_timezone");
		$timezone->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
				->setRequired(true)
				->addValidator('Timezone')
				->setLabel('Select Timezone:');
		$tzs = timezone_identifiers_list();
		foreach($tzs as $tz){
			$timezone->addMultiOption($tz,$tz);
		}
		$tzs = null;
		return $timezone;
	}
	
	/**
	 * Returns password element
	 * 
	 * @param string $form | The form name
	 * @param boolean $validatePasswordStrength = false | Include password strength validation?
	 * @return Zend_Form_Element_Password
	 */
	public static function password($form, $validatePasswordStrength = false)
	{
		$password = new Zend_Form_Element_Password("{$form}_password");
		$password->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
				->setRequired(true)
				->setLabel('Password:');
		if($validatePasswordStrength)
			$password->addValidator('PasswordStrength');
		return $password;
	}
	
	/**
	 * Returns confirm password elements
	 * 
	 * @param string $form | The form name
	 * @param boolean $required = true
	 * @return Zend_Form_Element_Password
	 */
	public static function confirmPassword($form, $required = true)
	{
		$password = new Zend_Form_Element_Password("{$form}_confirmPassword");
		$password->addPrefixPath('GEM_Validate', 'GEM/Validate/', 'validate')
				->setRequired($required)
				->setLabel('Confirm Password:')
				->addValidator('Confirm',false,array("{$form}_password",'messages' => array(
					'notMatch' => 'Passwords must match.'
				)));
		return $password;
	}
	
	/**
	 * Returns a hash element to prevent cross site
	 * request forgery (CSRF) attacks.
	 *  
	 * @param string $form | The form name
	 * @return Zend_Form_Element_Hash
	 */
	public static function noCSRF($form)
	{
		$noCSRF = new Zend_Form_Element_Hash("{$form}_noCSRF");
		$noCSRF->setDecorators(array('ViewHelper'));
		return $noCSRF;
	}
	
	/**
	 * Returns a captcha element to prevent bot spamming.
	 * 
	 * @param string $form | The form name
	 * @return Zend_Form_Element_Captcha
	 */
	public static function captcha($form)
	{
		$captcha = new Zend_Form_Element_Captcha("{$form}_captcha",array(
			'label' => 'Prove you are human',
			'captcha' => array(
				'captcha' => 'image',
				'wordLen' => 6,
				'timeout' => 300,
				'font'		=> APPLICATION_PATH . '/../fonts/calibri.ttf'		
			)	
		));
		return $captcha;
	}
	
	/**
	 * @param string $name | The text to appear on the button
	 * @return Zend_Form_Element_Button
	 */
	public static function submit($name = 'Submit')
	{
		$submit = new Zend_Form_Element_Button($name);
		$submit->setAttrib('type','submit')
				->setName($name)
				->setDecorators(array('ViewHelper',array('HtmlTag', array('tag' => '<div>','class' => 'submit'))));
		return $submit;
	}
	
	/**
	 * 
	 * @param Zend_Form $form
	 * @param GEM_Model_Domain_User $user |optional
	 * @return Zend_Form
	 */
	public static function setSocialSelectors($form,$user = null)
	{
		$socialTypes = Zend_Registry::get('config')->accounts->socialTypes->toArray();
		if(($user instanceof GEM_Model_Domain_User) && $user->hasSocial()){
			list($types,$values) = $user->getSocial();
		}
		$socialSelect = new Zend_Form_Element_Multiselect("{$form->getFormName()}_socialType");
			
	}
	
}